package ua.kharkov.knure.dereza.summarytask4.db.dao.mysql;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

import ua.kharkov.knure.dereza.summarytask4.db.dao.*;

/**
 * DAO factory for mysql.
 * 
 * @author dereza
 * 
 */
public class MysqlDAOFactory extends DAOFactory {

	private static final Logger log = Logger.getLogger(MysqlDAOFactory.class);
	
	/**
	 * Creates connection to MySql db.
	 * 
	 * @return Connection
	 */
	public static Connection createConnection(){
		Connection connection = null;
		try {
			Context initContext = new InitialContext();
			Context envContext = (Context) initContext.lookup("java:/comp/env");

			// SM - the name of data source
			DataSource ds = (DataSource) (envContext.lookup("jdbc/SM"));
			connection = ds.getConnection();
		} catch (NamingException ex) {
			log.error("Cannot init pool of connection", ex);
		} catch (SQLException ex) {
			log.error("Cannot get connection from Datasourse", ex);
		}
		return connection;
	}
	
	@Override
	public ClientDAO getClientDAO() {
		return new MysqlClientDAO();
	}
	
	@Override
	public CardDAO getCardDAO() {
		return new MysqlCardDAO();
	}
	
	@Override
	public RoleDAO getRoleDAO(){
		return new MysqlRoleDAO();
	}
	
	@Override
	public StatusDAO getStatusDAO(){
		return new MysqlStatusDAO();
	}
	
	@Override
	public CurrencyDAO getCurrencyDAO(){
		return new MysqlCurrencyDAO();
	}
	
	@Override
	public LocaleDAO getLocaleDAO(){
		return new MysqlLocaleDAO();
	}
}